<?php

class BWEvent extends BWEntityWithGroup {

    public $name;
    public $title;
    public $description;
    public $startTime;
    public $endTime;
    public $creator;
    public $messages = array();
    public $data = array();
    public $spaceId;
    public $appId;
    public $creatorId;
    protected $messageManager = null;
    protected $mediaManager = null;
    protected $inputMediaManager = null;

    public function getPropertyToColumnMapList($data = NULL) {
        $mapList = parent::getPropertyToColumnMapList($data);
        $mapList['startTime'] = 'start_time';
        $mapList['endTime'] = 'end_time';
        $mapList['creator'] = 'event_creator';
        $mapList['title'] = 'title';
        return $mapList;
    }

    public function addMessage($messageData, $data = NULL) {
        if (is_array($messageData)) {
            if (isset($messageData['id']) && $messageData['id'] == 0) {
                //create new message
                $message = new BWMessage();
                $message->name = isset($messageData['name']) ? $messageData['name'] : 'no-name';
                $message->save();
                //add message type
                if (isset($messageData['message_type'])) {
                    $typeName = 'MessageType.' . $messageData['message_type'];
                    $messageType = BWMessageGroup::getRoot()->getChild($typeName);
                    $messageType->addMember($message);
                }
            }
        }

        if ($messageData instanceof BWMessage) {
            $message = $messageData;
        }

        //add message into event
        if ($message instanceof BWMessage) {
            //add messageItem into message
            $row = BWMEventMessage::model()->findByAttributes(array('message_id' => $message->getId(), 'event_id' => $this->getId()));
            if ($row instanceof BWMEventMessage)
                return; //if find row, do nothing
            else { //add new row
                $eventMessageModel = new BWMEventMessage();
                $eventMessageModel->event_id = $this->getId();
                $eventMessageModel->message_id = $message->getId();
                $eventMessageModel->save();
            }
        }
    }

    /**
     * 
     * @param type $data
     * @return BWMessageManager
     */
    public function getMessageManager($data = NULL) {
        if ($this->messageManager == NULL)
            $this->messageManager = new BWMessageManager($this);
        return $this->messageManager;
    }

    /**
     * 
     * @param type $data
     * @return BWMediaManager
     */
    public function getMediaManager($data = NULL) {
        if ($this->mediaManager == NULL)
            $this->mediaManager = new BWMediaManager($this);
        return $this->mediaManager;
    }

    /**
     * 
     * @param type $data
     * @return BWInputMediaManager
     */
    public function getInputMediaManager($data = NULL) {
        if ($this->inputMediaManager == NULL)
            $this->inputMediaManager = new BWInputMediaManager($this);
        return $this->inputMediaManager;
    }

    public function getRepository($data = NULL, $reload = FALSE) {
        $repository = parent::getRepository($data, $reload);
        $repository->addPropertyToColumnMap('startTime', 'start_time');
        $repository->addPropertyToColumnMap('endTime', 'end_time');
        $repository->addPropertyToColumnMap('creator', 'event_creator');
        return $repository;
    }
    
    public function save($context = null) {
        if($this->spaceId == NULL||(is_numeric($this->spaceId)&&$this->spaceId<= 0))
            $this->spaceId = Blyn::app ()->getActiveSpace ()->getId ();
        if($this->appId == NULL||(is_numeric($this->appId)&&$this->appId<= 0))
            $this->appId = Blyn::app ()->getActiveApp ()->getId ();
        if($this->creatorId == NULL||(is_numeric($this->creatorId)&&$this->creatorId<= 0))
            $this->creatorId = Blyn::app ()->getCurrentUser ()->getId ();
        parent::save($context);
    }

}

?>
